package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.YddAdvimgDto;
import com.itheima.ydd.entity.YddAdvimg;
import com.itheima.ydd.entity.YddAdvpos;
import com.itheima.ydd.mapper.YddAdvimgMapper;
import com.itheima.ydd.service.YddAdvimgService;
import com.itheima.ydd.service.YddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 广告图业务层接口实现类
 */
@Service
public class YddAdvimgServiceImpl extends ServiceImpl<YddAdvimgMapper, YddAdvimg> implements YddAdvimgService {

    @Autowired
    private YddAdvposService advposService;

    /**
     * 分页条件查询 广告图,和对应广告位名称
     *
     * @param page      页码
     * @param limit     页面条目数
     * @param yddAdvimg 查询条件,广告图名,广告图描述
     * @return 分页查询对象
     */
    @Override
    public PageResult getList(Long page, Long limit, YddAdvimg yddAdvimg) {
        //创建分页查询对象,查询获取数据
        Page<YddAdvimg> pageInfo = new Page<>(page, limit);
        LambdaQueryWrapper<YddAdvimg> lqw=new LambdaQueryWrapper<>();
        lqw.like(yddAdvimg.getAdvimgdesc()!=null,YddAdvimg::getAdvimgdesc,yddAdvimg.getAdvimgdesc())
                .like(yddAdvimg.getAdvimgpos()!=null,YddAdvimg::getAdvimgpos,yddAdvimg.getAdvimgpos());
        this.page(pageInfo,lqw);

        List<YddAdvimgDto> advimgDtos = pageInfo.getRecords().stream().map(advimg -> {
            //创建dto对象,将广告图片信息复制到dto中
            YddAdvimgDto dto = new YddAdvimgDto();
            BeanUtils.copyProperties(advimg, dto);
            //获取该图片对应的广告位信息
            YddAdvpos advpos = advposService.getById(advimg.getAdvimgpos());
            //设置广告名
            dto.setAdvposname(advpos.getAdvposname());
            return dto;
        }).collect(Collectors.toList());

        //将数据封装到分页对象中并返回
        return PageResult.init(pageInfo.getTotal(), limit, page, advimgDtos);
    }



}
